package com.houor.device.controller.handler.codec;

import org.houor.utility.NumberUtility;

import com.houor.device.controller.data.ControllerFrame;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 输出帧编码
 * 
 * @author Houor
 *
 * @createTime 2017-04-20 22:52
 */
public class ControllerFrameEncoder extends MessageToByteEncoder<ControllerFrame> {

	@Override
	protected void encode(ChannelHandlerContext ctx, ControllerFrame msg, ByteBuf out) throws Exception {

		byte[] bsMsg = msg.toByteArray();

		// add frame length to frame
		out.writeBytes(NumberUtility.short2ByteArray((short) bsMsg.length));
		// then add frame
		out.writeBytes(bsMsg);
	}
}
